<?php
namespace app\pc\controller;

use app\common\service\UserService;
use app\pc\service\PuserService;
use app\common\lib\oss\Oss;

class User extends PcController
{
    
    //获取验证码
    public function getMobileCode(){
        $data = $this->request->get();
        //验证规则
        $validate = [
            'mobile'  => 'require|regex:/^1[3-9]\d{9}$/',
        ];
        //提示信息
        $message = [
            'mobile.require' => '手机号不能为空!',
            'mobile.regex' => '手机号格式不正确!',
        ];

        $this->validate($data, $validate, $message);

        if(UserService::getMobileCode($data['mobile'])){
            return success();
        }else{
            return failure(config('error.er4')['code'],config('error.er4')['msg']);
        }
    }

    //用户注册
    public function register(){
        $data = $this->request->post();
        $data['account'] = trim($data['account']);
        //验证规则
        $validate = [
            'account'  => 'require',
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
            'repassword'  => 'require',
            'mobile'  => 'require|regex:/^1[3-9]\d{9}$/',
            'code'  => 'require'
        ];
        //提示信息
        $message = [
            'account.require' => '账号不能为空!',
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
            'repassword.require' => '确认密码不能为空!',
            'mobile.require' => '手机号不能为空!',
            'mobile.regex' => '手机号格式不正确!',
            'code.require' => '验证码不能为空!',
        ];

        $this->validate($data, $validate, $message);

        //验证码不一致
        // if($data['code'] != config('cachekey.mobile_code').$data['mobile']){
        //     return failure(config('error.er7')['code'],config('error.er7')['msg']);
        // }

        //验证两次输入的密码是否一致
        if($data['password'] != $data['repassword']){
            return failure(config('error.er6')['code'],config('error.er6')['msg']);
        }

        if(PuserService::register($data)){
            return success();
        }else{
            return failure();
        }
    }

    //用户登录
    public function login(){
        $data = $this->request->post();
        if($data['type'] == 'mobile'){
             //验证规则
            $validate = [
                'mobile'  => 'require',
                'code'  => 'require'
            ];
            //提示信息
            $message = [
                'mobile.require' => '手机号不能为空!',
                'code.require' => '验证码不能为空!'
            ];
        }else{
            //账号密码登录
            //验证规则
            $validate = [
                'account'  => 'require',
                'password'  => 'require',
            ];
            //提示信息
            $message = [
                'account.require' => '账号不能为空!',
                'password.require' => '密码不能为空!',
            ];
        }
        
        $result = $this->validate($data, $validate, $message);
        if($result !== true){
            return failure(config('error.er5')['code'],$result->getError());
        }

        return $this->success(UserService::login($data));
    }

    //编辑用户信息
    public function edit(){
        $data = $this->request->post();
        $data['id'] = $this->user->id;
        if(UserService::update($data)){
            return success();
        }else{
            return failure();
        }
    }

    //更新密码
    public function updatePwd(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
            'repassword'  => 'require',
            'mobile'  => 'require|regex:/^1[3-9]\d{9}$/',
            'code'  => 'require'
        ];
        //提示信息
        $message = [
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
            'repassword.require' => '确认密码不能为空!',
            'mobile.require' => '手机号不能为空!',
            'mobile.regex' => '手机号格式不正确!',
            'code.require' => '验证码不能为空!',
        ];

        $data['id'] = $this->user->id;
        $result = $this->validate($data, $validate, $message);
        if($result !== true){
            return failure(config('error.er5')['code'],$result->getError());
        }

        //两次输入的密码不一致
        if($data['password'] !== $data['repassword']){
            return failure(config('error.er6')['code'],config('error.er6')['msg']);
        }

        // 验证手机验证码是否正确
        if($data['code'] != cache(MOBILE_CODE_.$data['mobile'])){
            return failure(config('error.er7')['code'],config('error.er7')['msg']);
        }

        if(UserService::updatePassword($data)){
            cache(MOBILE_CODE_.$data['mobile'],null);
            return success();
        }else{
            return failure();
        }
    }

    //上传头像
    public function updateAvatar(){
        $file = $this->request->post('file');
        $res =  Oss::uploadFile('test/1.txt',$file);
        if($res !== false){
            return success($res);
        }else{
            return failure();
        }
    }

    //签到
    public function sign(){
        $data['id'] = $this->user->id;
        
        if(UserService::sign($data)){
            return success();
        }else{
            return failure();
        }
    }

    //找回密码
    public function resetPwd(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
            'repassword'  => 'require',
            'mobile'  => 'require|regex:/^1[3-9]\d{9}$/',
            'code'  => 'require'
        ];
        //提示信息
        $message = [
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
            'repassword.require' => '确认密码不能为空!',
            'mobile.require' => '手机号不能为空!',
            'mobile.regex' => '手机号格式不正确!',
            'code.require' => '验证码不能为空!',
        ];

        $result = $this->validate($data, $validate, $message);
        if($result !== true){
            return failure(config('error.er5')['code'],$result->getError());
        }

        //两次输入的密码不一致
        if($data['password'] !== $data['repassword']){
            return failure(config('error.er6')['code'],config('error.er6')['msg']);
        }

        // 验证手机验证码是否正确
        if($data['code'] != cache(MOBILE_CODE_.$data['mobile'])){
            return failure(config('error.er7')['code'],config('error.er7')['msg']);
        }

        if(UserService::updatePasswordByMobile($data)){
            cache(MOBILE_CODE_.$data['mobile'],null);
            return success();
        }else{
            return failure();
        }
    }

}
